import { MSG_TYPES } from '../../utils/MESSAGE_TYPES'

export default function (xhr, PROXY_ROUTES) {
    return function (header, value) {
        const { url, setRequestHeader, bgReqId: id } = xhr
        // 判断是否需要代理
        if (PROXY_ROUTES.filter(({ enable }) => enable).some(({ matching }) => {
            const REG = new RegExp(matching, 'g')
            return REG.test(url)
        })) {
            window.postMessage({
                event: MSG_TYPES.setRequestHeader,
                id,
                data: {
                    header,
                    value
                }
            })
        } else {
            setRequestHeader.call(xhr, header, value)
        }
    }
}
